<template>
    <view style="padding-top: 5rpx; padding-bottom: 5rpx;">
        <u-navbar :custom-back="back" :title="title" :height="80">
            <template v-slot:right>
                <view class="u-flex u-flex-nowrap" style="max-width: 230rpx;" @click="usershow = !usershow">
                    <view class="u-flex">
                        <u-image mode="widthFix" width="50rpx" height="50rpx" src="/static/logo.png"></u-image>
                    </view>


                    <view style="margin: 10rpx 10rpx;" class="u-flex u-line-1 ">{{ user.username}}</view>
                </view>

            </template>
        </u-navbar>
        <u-popup mode="top" v-model="usershow">
            <view style="padding: 50rpx;">
                <u-avatar src="/static/logo.png" size="mini" ></u-avatar>
                <view style="margin-top: 50rpx;margin-bottom: 50rpx;font-size: 30rpx;font-weight: 500;">{{ user.username }}
                </view>
                <view></view>
                <u-button style="width: 85%;" type="primary" @click.stop="logOut()">退出</u-button>
            </view>
        </u-popup>
    </view>
</template>

<script setup lang="ts">
import { reactive, ref, getCurrentInstance, } from 'vue'
import { storeToRefs } from 'pinia'
import { useStore } from '@/store'

// 获取自定义的store
const store = useStore()
const usershow = ref(false)
// 取需要的 state
const { user } = storeToRefs(store)
const props = defineProps({
    item: {
        type: Object,
        default: () => {
            return {
            }
        }
    },
    title: {
        type: String,
        default: () => {
            return ""
        }
    },
    isShowTitle: {
        type: Boolean,
        default: () => {
            return false
        }
    },
    isShowSearch: {
        type: Boolean,
        default: () => {
            return false
        }
    },
    color: {
        type: String,
        default: () => {
            return 'transparent'
        }
    },
    isBackAction: {
        type: Boolean,
        default: () => {
            return false
        }
    },
    customBackAction: {
        type: Function,
        default: null
    }
})
const emits = defineEmits(["back"])

function back() {
    if (props.customBackAction != null) {
        props.customBackAction();
    } else {
        var pages = getCurrentPages() //获取加载的页面
        var currentPage = pages[pages.length - 1] //获取当前页面的对象
        var url = currentPage.route;

        switch (url) {
            case "pages/index/index":
                uni.showModal({
                    title: '提示',
                    content: '是否退出登录?',
                    success: (res) => {
                        if (res.confirm) {
                            uni.reLaunch({ url: '/pages/login/index' });
                        }
                    }
                }
                )

                break;

            default:
                if (pages.length == 1) {
                    if (url == 'pages/driverInformation/vehicleRegistrationInput')
                        uni.reLaunch({ url: '/pages/driverInformation/list' });
                    else if (url == 'pages/driverInformation/list') {
                        uni.reLaunch({ url: '/pages/index/index' });
                    }
                } else {
                    uni.navigateBack({
                        fail: () => {
                            uni.reLaunch({ url: '/pages/driverInformation/list' });
                        }
                    })
                }
                break;
        }
    }

}

const logOut = () => {
    store.loginOut();
}
</script>

<style lang="scss" scoped></style>
